package com.tianci.appstore.ui.list;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.coocaa.x.framework.utils.Android;
import com.coocaa.x.framework.utils.LogUtils;
import com.tianci.appstore.R;
import com.tianci.appstore.controller.manager.DeleteManagerController;
import com.tianci.appstore.ui.data.SkyAppBaseData;
import com.tianci.appstore.ui.data.SkyListAppData;
import com.tianci.appstore.ui.data.SkyListAppData.ListType;
import com.tianci.appstore.ui.list.uninstall.UninstallListView;
import com.tianci.appstore.ui.util.UICommon;
import com.tianci.appstore.ui.widget.TipToast;

import java.util.List;

/**
 * 列表页中的app块View
 * 使用此view的列表有：卸载列表
 **/

public class ListUninstallBlock extends ListBlock {
    protected TextView statusText;
    protected TextView sndInfoText;
    protected UninstallListView.UninstallStatus unstatus = UninstallListView.UninstallStatus.NOUNINSTALL;

    public ListUninstallBlock(Context context, ListType listType) {
        super(context, listType);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void update(SkyAppBaseData data) {
        // TODO Auto-generated method stub
        super.update(data);
//        SkyListStatusData statusData = (SkyListStatusData) data;
//        this.status = statusData.getStatus();
        boolean isOnSDCard = Android.isInstalledOnSDCard(mContext, ((SkyListAppData) data).getPkgName());
        LogUtils.d("", "sunny update:" + ((SkyListAppData) data).getPkgName());
        if (sndInfoText == null) {
            sndInfoText = UICommon.newTextView(mContext, infoTextSize, R.color.appInfoColor);
        }
        if (sndInfoText.getParent() == null) {
            rightLayout.addView(sndInfoText);
            rightLayout.setGravity(Gravity.TOP | Gravity.LEFT);
        }
        sndInfoText.setVisibility(View.VISIBLE);
        if (isOnSDCard) {
            sndInfoText.setText(mContext.getText(R.string.install_on_sdcard));
        } else {
            sndInfoText.setText(mContext.getText(R.string.install_on_local));
        }
    }


    @Override
    protected void toggleMultiSelected() {
        if (data == null)
            return;
        List<String> uninstallList = DeleteManagerController.getInstance().getUninstallList();
        String dataPkg = data.getPkgName();
        int listSize = uninstallList.size();
        if(uninstallList != null && uninstallList.size() > 0){
            for(int i=0; i<listSize; i++){
                String pkg = uninstallList.get(i);
                if(pkg != null && dataPkg != null && pkg.equals(dataPkg)){
                    String uninstallingStr;
                    if(i==0){
                        uninstallingStr = String.format(
                                mContext.getResources().getString(R.string.uninstalling_app),
                                data.getTitle());
                    }else{
                        uninstallingStr = mContext.getString(R.string.waituninstall_app);
                    }
                    TipToast.getInstance(mContext).showTipToast(uninstallingStr);
                    return;
                }
            }
        }
        super.toggleMultiSelected();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
    }

    protected String getStrIdByStatus(UninstallListView.UninstallStatus status) {
        String str = "";
        switch (status) {
            case NOUNINSTALL:
                break;
            case WAITUNINSTALL:
                str = mContext.getString(R.string.wait_uninstalling);
                break;
            case UNINSTALLING:
                str = mContext.getString(R.string.uninstalling_xx_app);
                break;
            case UNINSTALLED:
                str = mContext.getString(R.string.finish_uninstalling);
                break;
        }
        return str;
    }

    public void updateStatus(boolean isVisible, UninstallListView.UninstallStatus status) {
        unstatus = status;
        if(!isVisible && statusText != null){
            statusText.setText(getStrIdByStatus(status));
            statusText.setVisibility(View.GONE);
            return;
        }
        if (statusText == null) {
            statusText = UICommon.newTextView(mContext, statusTextSize, R.color.appuninstallStatusColor);
        }
        if (statusText.getParent() == null) {
            rightLayout.addView(statusText);
            rightLayout.setGravity(Gravity.TOP | Gravity.LEFT);
        }
        statusText.setText(getStrIdByStatus(status));
        statusText.setVisibility(View.VISIBLE);
    }

    @Override
    public void clearData() {
        // TODO Auto-generated method stub
        super.clearData();
        if (statusText != null) {
            statusText.setText("");
            statusText.setVisibility(View.GONE);
        }
    }
}
